package com.android.lovingpet.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.lovingpet.adopt.host.HostModel;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Body;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.mine.pet.PetModel;
import com.android.lovingpet.util.LocationClient;
import com.android.lovingpet.util.Utility;

import java.util.ArrayList;
import java.util.HashMap;

public class MinePresenter extends BasePresenter<IMineContract.View> implements IMineContract.Presenter {

    private PetModel petModel;
    private HostModel hostModel;
    private IMineContract.View view;
    private final int LOADING_PET=0;
    private final int LOADING_HOST=1;

    public MinePresenter(IMineContract.View view) {
        this.view = view;
        petModel=new PetModel();
        hostModel=new HostModel();
    }

    @Override
    public void sign() {
        MineModel.getInstance().sign(new Back() {
            @Override
            public void onFailure(String meg) {
                if (view==null) return;
                view.showToast(meg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getSignDays();
            }
        });
    }

    @Override
    public void isSign() {
        MineModel.getInstance().isSign(Utility.getAccount().getRealId(), new Back() {
            @Override
            public void onFailure(String meg) {
                //view.showToast(meg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                view.sign(MineModel.getInstance().getSignDays());
            }
        });
    }

    @Override
    public void getSignDays() {
        MineModel.getInstance().getSignDays(Utility.getAccount().getRealId(), new Back() {
            @Override
            public void onFailure(String meg) {
                if (view!=null) {
                    view.showToast(meg);
                }
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                int days = bundle.getInt("days");
                if (view!=null) {
                    view.sign(days);
                    view.showToast("签到成功，已连续签到" + days + "天");
                }
            }
        });
    }

    @Override
    public void getFansAndAttentions() {
        final Account account = Utility.getAccount();
        for (int i = 0; i < 2; i++) {
            if (account == null) {
                return;
            }
            UserModel.getInstance().getFansOrAttentions(account.getRealId(), i, new Back() {
                @Override
                public void onFailure(String meg) {
                    view.showToast(meg);
                }
                @Override
                public void onSuccess(@Nullable Bundle bundle) {
                    assert bundle != null;
                    ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                    if (bundle.getInt("mode") == 0) {
                        account.setFans(ids);
                    } else {
                        account.setLikes(ids);
                    }
                    account.saveOrUpdate();
                }
            });
        }
    }

    public void loadingInfo(int offset,int num)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        params.put("petType", String.valueOf(1));
     hostModel.sortByTime(params,new Back() {
         @Override
         public void onFailure(String msg) {
             getView().showToast(msg);
         }
         @Override
         public void onSuccess(@Nullable Bundle bundle) {
             if (bundle == null) {
                 return;
             }
             ArrayList<Integer> ids=bundle.getIntegerArrayList("ids");
             for (Integer id : ids) {
                 getInfo(id,LOADING_HOST);
             }
         }
     });
     petModel.sortByTime(params, new Back() {
         @Override
         public void onFailure(String msg) {
             getView().showToast(msg);
         }
         @Override
         public void onSuccess(@Nullable Bundle bundle) {
             ArrayList<Integer> ids=bundle.getIntegerArrayList("ids");
             if (ids!=null) {
                 for (Integer id : ids) {
                     getInfo(id, LOADING_PET);
                 }
             }
         }

     });
    }

    @Override
    public void getAddress(Body body) {
        HashMap<String,String> params=new HashMap<>();

        params.put("location",body.getLongitude()+","+body.getLatitude());
        LocationClient.regeo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                String address=bundle.getString("address");
                //Log.d("地址啊",address+"--/");
                body.setAddress(address);
                if (view==null) return;
                getView().addInfo(body);
            }
        });
    }

    private void getInfo(int id,int flag)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        Back back= new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
               if (bundle!=null) {
                   Body body = bundle.getParcelable("result");
                   if (body instanceof Pet)
                       Log.d("pet","i am pet");
                   else {
                       FileModel.getInstance().downloadImage(body.getId(), 3, new FileModel.FileBack() {
                           @Override
                           public void loadSuccess(String url) {
                               body.setImage(url);
                               if (getView()==null) {
                                   return;
                               }
                           }
                           @Override
                           public void onFailure(String msg) {

                           }

                           @Override
                           public void onSuccess(@Nullable Bundle bundle) {

                           }
                       });
                       Log.d("host", "i am host");
                   }

                  if (body==null)return;
                  // Log.d("地址经纬度",body.getLongitude()+","+body.getLatitude()+"**");
                   if (body.getLatitude()!=0&&body.getLongitude()!=0){
                       getAddress(body);
                   }
                   else{
                       body.setAddress("未知");
                       getView().addInfo(body);
                   }
               }
            }
        };
        if (flag==1) {
            hostModel.getHostInfo(params, back);
        }else {
            params.put("petType", String.valueOf(1));
            petModel.getPetInfo(params, back);
        }
    }

}
